﻿<template>
  <div class="app-container">
    <div class="chart-title">档案管理</div>
    <div class="chart-content">
      <div class="filter-title">
        <el-form label-position="right">
          <el-form-item>
            <el-select v-model="listQuery.status"
                       style="width: 160px; height:32px;"
                       clearable
                       :readonly="true"
                       :placeholder="$t('public.status')">
              <el-option v-for="item in statusOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="listQuery.order_no"
                      style="width: 160px; height:32px;"
                      class="filter-item"
                      :placeholder="$t('order.no')"
                      @keyup.enter.native="handleFilter"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="listQuery.equipment_name"
                      style="width: 160px; height:32px;"
                      class="filter-item"
                      :placeholder="$t('terminal.name')"
                      @keyup.enter.native="handleFilter"></el-input>
          </el-form-item>
          <div class="search-button"
               @click="handleFilter">
            <img src="@/assets/images/bg_search@2x.png" />
          </div>
          <div class="search-button"
               @click="handleRefresh">
            <img src="@/assets/images/bg_reset@2x.png" />
          </div>
          <div class="search-button"
               @click="searchItem">
            <img v-show="showSearchItem === true"
                 src="@/assets/images/bg_more@2x.png" />
            <img v-show="showSearchItem === false"
                 src="@/assets/images/bg_sx@2x.png" />
          </div>
        </el-form>
        <div class="filter-right">
          <div class="stock-button"
               :class="checkedRow.length > 0? '':'disable'">
            <button @click="handlePurchase">生成采购单</button>
          </div>
          <div class="add-button"
               @click="handleCreate">
            <img src="@/assets/images/icon_add@2x.png" />
            <span>新建</span>
          </div>
        </div>
      </div>
      <div v-show="showSearchItem"
           class="filter-container">
        <el-form>
          <el-form-item>
            <el-input v-model="listQuery.merchant_name"
                      style="width: 160px; height:32px;"
                      class="filter-item"
                      :placeholder="$t('business.name')"
                      @keyup.enter.native="handleFilter"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="listQuery.itoc_name"
                      style="width: 160px; height:32px;"
                      class="filter-item"
                      :placeholder="$t('operations.searchName')"
                      @keyup.enter.native="handleFilter"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="listQuery.supplier_name"
                      style="width: 160px; height:32px;"
                      class="filter-item"
                      :placeholder="$t('supplier.name')"
                      @keyup.enter.native="handleFilter"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="chart-list">
        <el-table :key="tableKey"
                  v-loading="listLoading"
                  :data="list"
                  element-loading-text="拼命加载中"
                  element-loading-spinner="el-icon-loading"
                  element-loading-background="rgba(255,255,255,0.2)"
                  border
                  fit
                  highlight-current-row
                  style="width: 100%"
                  @selection-change="handleSelectionChange">
          <el-table-column :selectable='isDisabled'
                           disabled="true"
                           align="center"
                           type="selection"
                           width="55">
          </el-table-column>
          <el-table-column fixed="left"
                           align="center"
                           :label="$t('table.id')"
                           width="65">
            <template slot-scope="scope">
              <span>{{
                (listQuery.page - 1) * listQuery.limit + scope.$index + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('order.no')">
            <template slot-scope="scope">
              <span>{{ scope.row.order_no }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('order.status')">
            <template slot-scope="scope">
              <span>{{ orderStatusEs(scope.row.status) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.creationTime')">
            <template slot-scope="scope">
              <span>{{ scope.row.create_time }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('order.time')">
            <template slot-scope="scope">
              <span>{{ scope.row.buy_time }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('operations.name_2')">
            <template slot-scope="scope">
              <span>{{ scope.row.itoc_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('supplier.name')">
            <template slot-scope="scope">
              <span>{{ scope.row.supplier_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('order.number')">
            <template slot-scope="scope">
              <span>{{ scope.row.number }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('order.sumPrice')">
            <template slot-scope="scope">
              <span>{{ scope.row.sum_price }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('table.processInformation')">
            <template slot-scope="scope">
              <el-button type="primary">{{ $t('table.processInformation') }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('table.viewLogistics')">
            <template slot-scope="scope">
              <el-button type="primary">{{ $t('table.viewLogistics') }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('table.installationStatus')">
            <template slot-scope="scope">
              <el-button type="primary"
                         :disabled="scope.row.status !== 4?true:false "
                         @click="installData(scope.row)">{{ $t('table.installationComplete') }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           :label="$t('table.actions')"
                           class-name="small-padding fixed-width"
                           fixed="right">
            <template slot-scope="scope">
              <div class="read-img"
                   @click="handleRead(scope.row)">
                <img src="@/assets/images/read@2x.png" />
              </div>
              <div class="read-img"
                   @click="handleModifyStatus(scope.row, 'deleted')">
                <img src="@/assets/images/delete@2x.png" />
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination background
                       :current-page="listQuery.page"
                       :page-sizes="[10, 20, 30, 50]"
                       :page-size="listQuery.limit"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"></el-pagination>
      </div>
      <!-- 编辑 -->
      <el-dialog :title="textMap[dialogStatus]"
                 :visible.sync="dialogFormVisible">
        <el-form ref="dataForm"
                 :rules="rules"
                 :model="temp"
                 label-position="right"
                 label-width="35%"
                 style="width: 400px margin-left:120px"
                 size="small">
          <div class="pops">
            <div class="information-title">
              <span class="icon-title"></span>
              <span>基础信息:</span>
            </div>
            <div class="pops-item">
              <div class="pops-left">
                <el-form-item :label="$t('business.name') + ' :'"
                              prop="merchant_id">
                  <drop-down-select :data="merchantOptions"
                                    :count="merchantCount"
                                    :page-size="pageSize"
                                    :disabled="dialogStatus === 'read' ? true : false"
                                    :id="temp.merchant_id"
                                    :name="temp.merchant_name"
                                    @getList="(pageNumber, search)=>{return getMerchant(pageNumber, search,'getMerchant')}"
                                    @addList="(pageNumber, search)=>{return getMerchant(pageNumber, search,'addMerchant')}"
                                    @choose="(data)=>{return choose(data, 'merchant')}"
                                    @remove="(data)=>{return remove(data, 'merchant')}"></drop-down-select>
                </el-form-item>
                <el-form-item :label="$t('terminal.type') + ' :'"
                              prop="equipment_id">
                  <drop-down-select :data="terminalOptions"
                                    :count="terminalCount"
                                    :page-size="pageSize"
                                    :disabled="dialogStatus === 'read' ? true : false"
                                    :id="temp.equipment_id"
                                    :name="temp.equipment_name"
                                    @getList="(pageNumber, search)=>{return getTerminal(pageNumber, search,'getTerminal')}"
                                    @addList="(pageNumber, search)=>{return getTerminal(pageNumber, search,'addTerminal')}"
                                    @choose="(data)=>{return choose(data, 'terminal')}"
                                    @remove="(data)=>{return remove(data, 'terminal')}"></drop-down-select>
                </el-form-item>
                <el-form-item :label="$t('businessDistrict.name') + ' :'"
                              prop="merchant_circle_name">
                  <drop-down-select :data="circleOptions"
                                    :count="circleCount"
                                    :page-size="pageSize"
                                    :disabled="dialogStatus === 'read' ? true : false"
                                    :id="temp.merchant_circle_id"
                                    :name="temp.merchant_circle_name"
                                    @getList="(pageNumber, search)=>{return getCircle(pageNumber, search,'getCircle')}"
                                    @addList="(pageNumber, search)=>{return getCircle(pageNumber, search,'addCircle')}"
                                    @choose="(data)=>{return choose(data, 'circle')}"
                                    @remove="(data)=>{return remove(data, 'circle')}"></drop-down-select>
                </el-form-item>
                <el-form-item :label="$t('partner.name') + ' :'"
                              prop="partner_id">
                  <drop-down-select :data="partnerOptions"
                                    :count="partnerCount"
                                    :page-size="pageSize"
                                    :disabled="dialogStatus === 'read' ? true : false"
                                    :id="temp.partner_id"
                                    :name="temp.partner_name"
                                    @getList="(pageNumber, search)=>{return getPartner(pageNumber, search,'getPartner')}"
                                    @addList="(pageNumber, search)=>{return getPartner(pageNumber, search,'addPartner')}"
                                    @choose="(data)=>{return choose(data, 'partner')}"
                                    @remove="(data)=>{return remove(data, 'partner')}"></drop-down-select>
                </el-form-item>
                <el-form-item :label="$t('maintain.name') + ' :'"
                              prop="maintain_id">
                  <drop-down-select :data="maintainOptions"
                                    :count="maintainCount"
                                    :page-size="pageSize"
                                    :disabled="dialogStatus === 'read' ? true : false"
                                    :id="temp.maintain_id"
                                    :name="temp.maintain_name"
                                    @getList="(pageNumber, search)=>{return getMaintain(pageNumber, search,'getMaintain')}"
                                    @addList="(pageNumber, search)=>{return getMaintain(pageNumber, search,'addMaintain')}"
                                    @choose="(data)=>{return choose(data, 'maintain')}"
                                    @remove="(data)=>{return remove(data, 'maintain')}"></drop-down-select>
                </el-form-item>
              </div>
              <div class="pops-right">
                <el-form-item :label="$t('goods.number') + ' :'"
                              prop="number">
                  <el-input-number class="left-number"
                                   v-model="temp.number"
                                   :controls="false"
                                   :min="0"
                                   :disabled="dialogStatus === 'read' ? true : false"></el-input-number>
                </el-form-item>
                <el-form-item :label="$t('goods.linkman') + ' :'"
                              prop="linkman">
                  <el-input v-model="temp.linkman"
                            :readonly="dialogStatus === 'read' ? true : false"></el-input>
                </el-form-item>
                <el-form-item :label="$t('goods.linkmanMobile') + ' :'"
                              prop="linkman_mobile">
                  <el-input v-model="temp.linkman_mobile"
                            :readonly="dialogStatus === 'read' ? true : false"></el-input>
                </el-form-item>
                <el-form-item :label="$t('goods.address') + ' :'"
                              prop="address">
                  <el-input v-model="temp.address"
                            :readonly="dialogStatus === 'read' ? true : false"></el-input>
                </el-form-item>
                <el-form-item :label="$t('public.remark') + ' :'"
                              prop="remark">
                  <el-input v-model="temp.remark"
                            type="textarea"
                            :readonly="dialogStatus === 'read' ? true : false"></el-input>
                </el-form-item>
              </div>
            </div>
          </div>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
          <div>
            <el-button v-show="dialogStatus == 'read'"
                       type="primary"
                       @click="dialogStatus = 'edit'">{{ $t("table.edit") }}</el-button>
          </div>
          <div>
            <el-button @click="dialogFormVisible = false">{{
              $t("table.cancel")
            }}</el-button>
            <el-button v-if="dialogStatus == 'create'"
                       type="primary"
                       :disabled="!createDisabled"
                       @click="createData">{{ $t("table.confirm") }}</el-button>
            <el-button v-else-if="dialogStatus == 'edit'"
                       type="primary"
                       :disabled="!updateDisabled"
                       @click="updateData">{{ $t("table.confirm") }}</el-button>
            <el-button v-else
                       type="primary"
                       @click="dialogFormVisible = false">{{ $t("table.confirm") }}</el-button>
          </div>
        </div>
      </el-dialog>
      <!-- 查看采购单 -->
      <el-dialog :title="textMap[dialogStatus]"
                 :visible.sync="dialogChartFormVisible">
        <el-form ref="reviewForm"
                 :rules="reviewRules"
                 :model="temp"
                 size="small">
          <div class="popup-form">
            <div class="headerless-form">
              <div class="from-title">订单编号:&nbsp; {{ temp.order_no }}</div>
              <div class="from-content">
                <div>
                  订单状态:&nbsp; {{ orderStatusEs(temp.status) }}
                </div>
                <div>
                  创建时间：&nbsp; {{ temp.create_time }}
                </div>
                <div>
                  下单时间:&nbsp; {{ temp.buy_time }}
                </div>
              </div>
            </div>
            <div class="from-banner"></div>
            <div class="header-form">
              <div class="from-title">收货人信息</div>
              <div class="from-item">
                <div class="from-header">
                  <div>商家</div>
                  <div>联系人</div>
                  <div>联系电话</div>
                  <div>地址</div>
                </div>
                <div class="from-content">
                  <div>{{ temp.merchant_name }}</div>
                  <div>{{ temp.linkman }}</div>
                  <div>{{ temp.linkman_mobile }}</div>
                  <div>{{ temp.address }}
                  </div>
                </div>
              </div>
            </div>
            <div class="header-form">
              <div class="from-title">供货人信息</div>
              <div class="from-item">
                <div class="from-header">
                  <div>商家</div>
                  <div>联系人</div>
                  <div>联系电话</div>
                  <div>地址</div>
                </div>
                <div class="from-content">
                  <div>{{ temp.supplier_name }}</div>
                  <div>{{ temp.supplier_linkman }}</div>
                  <div>{{ temp.supplier_linkman_mobile }}</div>
                  <div>{{ temp.supplier_address }}</div>
                </div>
              </div>
            </div>
            <div class="from-banner"></div>
            <div class="header-form">
              <div class="from-item">
                <div class="from-header">
                  <div>终端名称</div>
                  <div>终端类别</div>
                  <div>品牌</div>
                  <div>型号</div>
                  <div>单价</div>
                  <div>商圈</div>
                </div>
                <div class="from-content">
                  <div>{{ temp.equipment_name }}</div>
                  <div>{{ temp.category_name }}</div>
                  <div>{{ temp.brand_name }}</div>
                  <div>{{ temp.attribute }}</div>
                  <div>{{ temp.price }}</div>
                  <div>{{ temp.assess_turnover }}</div>
                </div>
                <div class="from-footer">
                  <div class="remark">备注信息: <span class="remark-content">{{ temp.remark }}</span></div>
                  <div class="total">
                    <div>设备：{{ temp.number }}台</div>
                    <div>总计：{{ temp.sum_price }}</div>
                  </div>
                </div>
              </div>
            </div>
            <div class="headerless-form">
              <div class="from-title">审核备注</div>

              <div class="from-item">
                <el-form-item prop="check_remark">
                  <el-input v-model="temp.check_remark"
                            type="textarea"
                            :readonly="dialogStatus === 'review'? false:true"></el-input>
                </el-form-item>
              </div>
            </div>
          </div>
        </el-form>
        <div class="footer-banner">
        </div>
        <div slot="footer"
             class="dialog-footer between-space">
          <div>
            <el-button v-show="dialogStatus == 'read'"
                       type="primary"
                       @click="printData">{{ $t("table.print") }}</el-button>
            <el-button v-show="dialogStatus == 'read' && temp.status === 1"
                       type="primary"
                       @click="handlereview">{{ $t("table.review") }}</el-button>
            <el-button v-show="dialogStatus === 'review' && temp.status === 1"
                       type="primary"
                       @click="reviewData(6)">{{ $t("table.reject") }}</el-button>
          </div>
          <div>
            <el-button @click="dialogChartFormVisible = false">{{
              $t("table.cancel")
            }}</el-button>

            <el-button v-if="dialogStatus === 'review' && temp.status === 1"
                       type="primary"
                       @click="reviewData(2)">{{ $t("table.pass") }}</el-button>
            <el-button v-else
                       type="primary"
                       @click="dialogChartFormVisible = false">{{ $t("table.confirm") }}</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script type="text/javascript">
import dropDownSelect from "@/views/select/dropDownSelect"; // 分页选择控件
import { getToken } from "@/utils/auth";
export default {
  name: "Itoc",
  components: {
    "drop-down-select": dropDownSelect
  },
  data() {
    const validateRemark = (rule, value, callback) => {
      if (value) {
        if (value.length >= 2 && value.length < 100) {
          callback();
        } else {
          callback(new Error("备注字符数不少于2字符且不大于100字符"));
        }
      } else {
        callback();
      }
    };
    const validateAddress = (rule, value, callback) => {
      if (value) {
        if (value.length > 2 && value.length < 100) {
          callback();
        } else {
          callback(new Error("详细收货地址字符数不少于2字符且不大于100字符"));
        }
      } else {
        callback(new Error("请填写详细收货地址"));
      }
    };
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      checkedRow: [],
      statusOptions: [{
        value: 1,
        label: '待审核'
      }, {
        value: 2,
        label: '待采购'
      }, {
        value: 3,
        label: '待发货'
      }, {
        value: 4,
        label: '待收货'
      }, {
        value: 5,
        label: '已完成'
      }, {
        value: 6,
        label: '驳回'
      }],
      listQuery: {
        page: 1,
        limit: 20,
        status: "",
        order_no: "",
        equipment_name: "",
        merchant_name: "",
        itoc_name: "",
        supplier_name: ""
      },
      temp: {
        id: undefined,
        linkman: "",
        linkman_mobile: "",
        address: "",
        number: undefined,
        equipment_id: undefined,
        equipment_name: "",
        partner_id: undefined,
        partner_name: "",
        merchant_circle_id: undefined,
        merchant_circle_name: "",
        merchant_id: undefined,
        merchant_name: "",
        remark: "",
        maintain_id: undefined,
        maintain_name: ""
      },
      purchaseOrder: undefined,
      dialogFormVisible: false,
      dialogChartFormVisible: false,
      dialogPurchaseOrderVisible: false,
      dialogStatus: "",
      textMap: {
        edit: "编辑",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      rules: {
        equipment_id: [{ required: true, message: "请填写货道容量", trigger: "change" }],
        merchant_circle_name: [{ required: true, message: "请选择商圈", trigger: "change" }],
        linkman: [{ required: true, message: "请填写收货人", trigger: "blur" }],
        linkman_mobile: [{ required: true, message: "请填写收货人电话", trigger: "blur" }],
        address: [{ required: true, trigger: "blur", validator: validateAddress }],
        number: [{ required: true, message: "请填写申请数量", trigger: "blur" }],
        remark: [{ trigger: "blur", validator: validateRemark }],
        partner_id: [{ required: true, message: "请填写申请数量", trigger: "change" }]
      },
      reviewRules: {
        check_remark: [{ trigger: "blur", validator: validateRemark }]
      },
      createDisabled: true,
      updateDisabled: true,
      brandOptions: [],
      brandCount: undefined,
      circleOptions: undefined,
      circleCount: undefined,
      sortOptions: [],
      sortCount: undefined,
      terminalOptions: [],
      terminalCount: undefined,
      merchantOptions: [],
      merchantCount: undefined,
      partnerOptions: [],
      partnerCount: undefined,
      maintainOptions: [],
      maintainCount: undefined,
      pageSize: 20,
      optionProps: {
        value: 'id',
        label: 'name'
      }
    };
  },
  created() {
    const token = getToken()
    if (token && !this.$store.state.socket.loginTransition && !this.$store.state.socket.loginAccount) {
      this.tokenLogin(token)
    }
    this.getList();
  },
  methods: {
    tokenLogin(token) {
      const path = {
        app: "wxadmin",
        api: "api_wxadmin_admin_keepLogin",
        version: "1.0",
        type: 2,
        data: {
          token: token
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        this.socketData(res);
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        status: "",
        order_no: "",
        equipment_name: "",
        merchant_name: "",
        itoc_name: "",
        supplier_name: ""
      };
      this.getList();
    },
    handlereview() {
      this.dialogStatus = 'review'
    },
    searchItem() {
      if (this.showSearchItem === true) {
        this.showSearchItem = false;
      } else {
        this.showSearchItem = true;
      }
    },
    // 获取终端申请列表
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page,
        name: this.listQuery.name ? this.listQuery.name : "",
        status: this.listQuery.status ? this.listQuery.status : "",
        order_no: this.listQuery.order_no ? this.listQuery.order_no : "",
        equipment_name: this.listQuery.equipment_name ? this.listQuery.equipment_name : "",
        merchant_name: this.listQuery.merchant_name ? this.listQuery.merchant_name : "",
        itoc_name: this.listQuery.itoc_name ? this.listQuery.itoc_name : "",
        supplier_name: this.listQuery.supplier_name ? this.listQuery.supplier_name : ""
      };
      const path = {
        app: "archives",
        api: "api_equipment_index_getRequestList",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    // 打开查看弹窗
    handleRead(row) {
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogChartFormVisible = true;
      this.$nextTick(() => {
        this.$refs["reviewForm"].clearValidate();
      });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        linkman: "",
        linkman_mobile: "",
        address: "",
        number: undefined,
        equipment_id: undefined,
        equipment_name: "",
        partner_id: undefined,
        partner_name: "",
        merchant_circle_id: undefined,
        merchant_circle_name: "",
        merchant_id: undefined,
        merchant_name: "",
        remark: "",
        maintain_id: undefined,
        maintain_name: ""
      };
    },
    // 更新数据
    upData() {
      const data = {
        id: this.temp.id ? this.temp.id : "",
        linkman: this.temp.linkman ? this.temp.linkman : "",
        linkman_mobile: this.temp.linkman_mobile ? this.temp.linkman_mobile : "",
        address: this.temp.address ? this.temp.address : "",
        number: this.temp.number ? this.temp.number : "",
        equipment_id: this.temp.equipment_id ? this.temp.equipment_id : "",
        equipment_name: this.temp.equipment_name ? this.temp.equipment_name : "",
        partner_id: this.temp.partner_id ? this.temp.partner_id : "",
        partner_name: this.temp.partner_name ? this.temp.partner_name : "",
        merchant_circle_id: this.temp.merchant_circle_id ? this.temp.merchant_circle_id : "",
        merchant_circle_name: this.temp.merchant_circle_name ? this.temp.merchant_circle_name : "",
        merchant_id: this.temp.merchant_id ? this.temp.merchant_id : "",
        merchant_name: this.temp.merchant_name ? this.temp.merchant_name : "",
        remark: this.temp.remark ? this.temp.remark : "",
        maintain_id: this.temp.maintain_id ? this.temp.maintain_id : "",
        maintain_name: this.temp.maintain_name ? this.temp.maintain_name : ""
      };
      return data;
    },
    // 添加终端申请档案
    createData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.createDisabled) {
            this.createDisabled = false;
            const data = this.upData();
            const path = {
              app: "archives",
              api: "api_equipment_index_add",
              version: "1.0",
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(path), res => {
              if (res) {
                this.socketData(res);
              }
              setTimeout(() => {
                this.createDisabled = true;
              }, 1000);
            });
          }
        }
      });
    },
    // 更新终端档案
    updateData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.updateDisabled) {
            this.updateDisabled = false;
            const data = this.upData();
            const path = {
              app: "equipment",
              api: "api_equipment_index_requestCheck",
              version: "1.0",
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(path), res => {
              if (res) {
                this.socketData(res);
              }
              setTimeout(() => {
                this.updateDisabled = true;
              }, 1000);
            });
          }
        }
      });
    },
    handleModifyStatus(row) {
      this.$confirm("此操作将永久删除该终端信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const path = {
            app: "archives",
            api: "api_archives_equipment_delete",
            version: "1.0",
            data: {
              id: row.id
            }
          };
          this.socketApi.sendSock(JSON.stringify(path), res => {
            if (res) {
              this.socketData(res);
            } else {
              this.$message.error(" 删除失败 ");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    isDisabled(row, index) {
      if (row.status === 2) {
        return 1
      } else {
        return 0
      }
    },
    handleSelectionChange(data) {
      const checkedRow = []
      if (data.length > 0) {
        data.forEach(element => {
          checkedRow.push(element.id);
        });
      }
      this.checkedRow = checkedRow
    },
    // 打印
    printData() {
      console.log('打印')
    },
    // 通过/驳回
    reviewData(type) {
      if (type === 6 && !(this.temp.check_remark)) {
        this.$message.error('驳回必须添加审核备注');
        return false;
      }
      this.$refs["reviewForm"].validate(valid => {
        if (valid) {
          const path = {
            app: "equipment",
            api: "api_equipment_index_requestCheck",
            version: "1.0",
            data: {
              id: this.temp.id ? this.temp.id : "",
              status: type,
              check_remark: this.temp.check_remark ? this.temp.check_remark : "",
              returnType: type
            }
          };
          this.socketApi.sendSock(JSON.stringify(path), res => {
            if (res) {
              this.socketData(res);
            }
          });
        }
      });
    },
    // 生成采购单
    handlePurchase() {
      if (this.checkedRow.length > 0) {
        this.$router.push({
          path: '/terminal-data/terminal-purchase-order',
          query: {
            ids: this.checkedRow.join(',')
          }
        })
      } else {
        return false
      }
    },
    // 安装完成
    installData(data) {
      const path = {
        app: "equipment",
        api: "api_equipment_index_requestStatusEdit",
        version: "1.0",
        data: {
          id: data.id
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 商家选择
    getMerchant(pageNumber, search, name) {
      const path = {
        app: "common",
        api: "api_archives_merchant_getList",
        version: "1.0",
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || "",
          type: 2
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 设备类型选择
    getTerminal(pageNumber, search, name) {
      const path = {
        app: "common",
        api: "api_archives_equipment_getList",
        version: "1.0",
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || "",
          type: 2
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 商圈选择
    getCircle(pageNumber, search, name) {
      const path = {
        app: "common",
        api: "api_archives_merchantCircle_getList",
        version: "1.0",
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || ""
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 合伙人选择
    getPartner(pageNumber, search, name) {
      const path = {
        app: "archives",
        api: "api_archives_partner_getList",
        version: "1.0",
        type: 2,
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || ""
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 维护人
    getMaintain(pageNumber, search, name) {
      const path = {
        app: "archives",
        api: "api_archives_maintain_getList",
        version: "1.0",
        type: 2,
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || ""
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 选择分类
    chooseSort(e) {
      const nodeData = this.$refs['cascaderSort'].getCheckedNodes()[0].data
      this.temp.category_id = nodeData.id;
      this.temp.category_name = nodeData.name;
    },
    // 供货商选择
    getSupplier(pageNumber, search, name) {
      const path = {
        app: "common",
        api: "api_archives_supplier_getList",
        version: "1.0",
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || ""
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    choose(data, name) {
      if (name === 'circle') {
        this.temp.merchant_circle_id = data.id;
        this.temp.merchant_circle_name = data.name;
      } else if (name === 'terminal') {
        this.temp.equipment_id = data.id;
        this.temp.equipment_name = data.name;
      } else if (name === 'merchant') {
        this.temp.merchant_id = data.id;
        this.temp.merchant_name = data.name;
      } else if (name === 'partner') {
        this.temp.partner_id = data.id;
        this.temp.partner_name = data.name;
      } else if (name === 'maintain') {
        this.temp.maintain_id = data.id;
        this.temp.maintain_name = data.name;
      }
      this.calibration('dataForm');
    },
    remove(data, name) {
      if (name === 'circle') {
        this.temp.merchant_circle_id = undefined;
        this.temp.merchant_circle_name = "";
      } else if (name === 'terminal') {
        this.temp.equipment_id = undefined;
        this.temp.equipment_name = "";
      } else if (name === 'merchant') {
        this.temp.merchant_id = undefined;
        this.temp.merchant_name = "";
      } else if (name === 'partner') {
        this.temp.partner_id = undefined;
        this.temp.partner_name = "";
      } else if (name === 'maintain') {
        this.temp.maintain_id = undefined;
        this.temp.maintain_name = "";
      }
      this.calibration('dataForm');
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "api_wxadmin_admin_keepLogin") {
            this.$store.dispatch("loginTransition", true);
            this.socketApi.sendCache(res => {
              this.socketData(res);
            })
          } else if (resj.api === "api_equipment_index_getRequestList") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "api_equipment_index_add") {
            this.$notify({
              title: "成功",
              message: "申请成功",
              type: "success",
              duration: 2000
            });
            this.dialogFormVisible = false;
            this.getList();
          } else if (resj.api === "api_archives_equipment_delete") {
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success",
              duration: 2000
            });
            this.dialogFormVisible = false;
            this.getList();
          } else if (resj.api === "api_equipment_index_requestCheck" && resj.type === 6) {
            this.$notify({
              title: "成功",
              message: "驳回成功",
              type: "success",
              duration: 2000
            });
            this.dialogChartFormVisible = false;
            this.getList();
          } else if (resj.api === "api_equipment_index_requestCheck" && resj.type === 2) {
            this.$notify({
              title: "成功",
              message: "通过成功",
              type: "success",
              duration: 2000
            });
            this.dialogChartFormVisible = false;
            this.getList();
          } else if (resj.api === "api_equipment_index_requestStatusEdit") {
            this.$notify({
              title: "成功",
              message: "确认收货成功",
              type: "success",
              duration: 2000
            });
            this.getList();
          } else if (resj.api === "api_archives_category_getList") {
            this.sortOptions = resj.data;
          } else if (resj.api === "api_archives_merchantCircle_getList" && resj.type === "getCircle") {
            this.circleOptions = resj.data;
            this.circleCount = resj.count;
          } else if (resj.api === "api_archives_merchantCircle_getList" && resj.type === "addCircle") {
            this.circleOptions = this.circleOptions.concat(resj.data);
          } else if (resj.api === "api_archives_equipment_getList" && resj.type === "getTerminal") {
            this.terminalOptions = resj.data;
            this.terminalCount = resj.count;
          } else if (resj.api === "api_archives_equipment_getList" && resj.type === "addTerminal") {
            this.terminalOptions = this.terminalOptions.concat(resj.data);
          } else if (resj.api === "api_equipment_index_createEquipmentOrder") {
            this.purchaseOrder = resj.data
            this.dialogPurchaseOrderVisible = true
          } else if (resj.api === "api_archives_merchant_getList" && resj.type === "getMerchant") {
            this.merchantOptions = resj.data;
            this.merchantCount = resj.count;
          } else if (resj.api === "api_archives_merchant_getList" && resj.type === "addMerchant") {
            this.merchantOptions = this.merchantOptions.concat(resj.data);
          } else if (resj.api === "api_archives_partner_getList" && resj.type === "getPartner") {
            this.partnerOptions = resj.data;
            this.partnerCount = resj.count;
          } else if (resj.api === "api_archives_partner_getList" && resj.type === "addPartner") {
            this.partnerOptions = this.merchantOptions.concat(resj.data);
          } else if (resj.api === "api_archives_maintain_getList" && resj.type === "getMaintain") {
            this.maintainOptions = resj.data;
            this.maintainCount = resj.count;
          } else if (resj.api === "api_archives_maintain_getList" && resj.type === "addMaintain") {
            this.maintainOptions = this.maintainOptions.concat(resj.data);
          }
        }
      }
    },
    calibration(name) {
      this.$refs[name].validate(valid => {
      })
    }
  }
};
</script>
